package com.ojoin.trade.common_core.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.common_core.web.dao.TbMerchantInfoCheckDao;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfoCheck;
import com.ojoin.trade.common_core.web.service.MerchantUserBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantInfoCheckService;

/**
 * 实名认证校验 服务实现
 * @author guoting
 *
 */
@Service
public class TbMerchantInfoCheckServiceImpl extends BaseServiceImpl<TbMerchantInfoCheck> implements
	TbMerchantInfoCheckService {

	@Autowired
	private MerchantUserBaseService userservice;
	
	
	@Autowired
	private TbMerchantInfoCheckDao dao;
	
	@Override
	protected BaseDao<TbMerchantInfoCheck> getBaseDao() {
		return dao;
	}

	@Override
	public int updateCheckFlagByJoinuserId(int joinuserid, String type, int flagvalue)
			throws BusinessException {
		LogUtils.getT0loanLog().info("更新状态. joinid->" + joinuserid);
		
//		userservice.updateShortnameById(joinuserid, "test");
		
		TbMerchantInfoCheck checkvo = this.selectByJoinuserId(joinuserid);
		int count = 0;
		if (checkvo == null) {
			checkvo = new TbMerchantInfoCheck();
			setCheckFlag(joinuserid, type, flagvalue, checkvo);
			 ((TbMerchantInfoCheckDao) this.getBaseDao()).insert(checkvo);
			 return checkvo.getId();
		} else {
			setCheckFlag(joinuserid, type, flagvalue, checkvo);
			count = ((TbMerchantInfoCheckDao) this.getBaseDao()).updateCheckFlagByJoinuserId(checkvo);
		}
		
//		if (count != -1)
//			throw new BusinessException("测试事物回滚.");
		
		return count;
    	
	}

	private void setCheckFlag(int joinuserid, String type, int flagvalue, TbMerchantInfoCheck checkvo) {
		checkvo.setIdOfTbMerchantUser(joinuserid);
    	if (type.equalsIgnoreCase("yyzz")) {
    		checkvo.setYyzzCheckFlag(flagvalue);
    	} else if (type.equalsIgnoreCase("swdjz")) {
    		checkvo.setSwdjzCheckFlag(flagvalue);
    	} else if (type.equalsIgnoreCase("zzjgdm")) {
    		checkvo.setZzjgdmCheckFlag(flagvalue);
    	} else if (type.equalsIgnoreCase("khxkz")) {
    		checkvo.setKhxkzCheckFlag(flagvalue);
    	} else if (type.equalsIgnoreCase("frsfz")) {
    		checkvo.setFrsfzCheckFlag(flagvalue);
    	} else if (type.equalsIgnoreCase("khhzd")) {
    		checkvo.setKhhzdCheckFlag(flagvalue);
    	}
	}

	@Override
	public TbMerchantInfoCheck selectByJoinuserId(int joinid)
			throws BusinessException {
		LogUtils.getT0loanLog().info("查询详细信息：joinid->" + joinid);
		return ((TbMerchantInfoCheckDao) this
				.getBaseDao()).selectByJoinuserId(joinid);
	}

	@Override
	public void deleteByJoinuserId(int joinid) throws BusinessException {
		LogUtils.getT0loanLog().info("查询详细信息：joinid->" + joinid);
		((TbMerchantInfoCheckDao) this
				.getBaseDao()).deleteByJoinuserId(joinid);
	}

}
